package org.example.dao.imp;

import org.example.dao.inter.UserDao;
import org.example.entitys.systems.Permission;
import org.example.entitys.systems.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @Description
 * @Author 魏锴
 * @Date 2023/11/12
 */

@Repository
public class UserDaoImp implements UserDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int insert(User user) {
        String sql = "insert into t_sys_user(login_name, password, gender, birthday," +
                " phone, email, status) values (?, ?, ?, ?, ?, ?, ? )";
        return jdbcTemplate.update(sql,
                user.getName(),
                user.getPassword(),
                user.getGender(),
                user.getBirthday(),
                user.getPhone(),
                user.getEmail(),
                user.getStatus()
        );
    }

    @Override
    public int update(User user) {
        String sql = "update t_sys_user set login_name = ?, password = ?, gender = ?, " +
                "birthday = ?, phone = ?, email = ?, status = ? where id = ?";
        return jdbcTemplate.update(sql,
                user.getName(),
                user.getPassword(),
                user.getGender(),
                user.getBirthday(),
                user.getPhone(),
                user.getEmail(),
                user.getStatus(),
                user.getId()
        );

    }

    @Override
    public int delete(int id) {
        String sql = "delete from t_sys_user where id = ?";
        return jdbcTemplate.update(sql, id);
    }

    @Override
    public boolean isExitById(int id) {
        String sql = "select count(*) from t_sys_user where id = ?";
        return jdbcTemplate.queryForObject(sql, Integer.class, id) > 0;
    }

    @Override
    public User queryById(int id) {
        String sql = "select id,login_name,password,gender,birthday,phone," +
                "email,status from t_sys_user where id = ?";
        return jdbcTemplate.queryForObject(sql, this::transform, id);
    }

    @Override
    public int queryLastInsertID() {
        String sql = "select LAST_INSERT_ID()";
        return jdbcTemplate.queryForObject(sql, Integer.class);
    }

    @Override
    public List<User> queryAll() {
        String sql = "select `id`,`login_name`,`password`,`gender`,`birthday`,`phone`," +
                "`email`,`status` from t_sys_user";
        return jdbcTemplate.query(sql, this::transform);
    }


    @Override
    public List<User> queryByName(String name) {
        String sql = "select `id`,`login_name`,`password`,`gender`,`birthday`,`phone`," +
                "`email`,`status` from t_sys_user where login_name = ? ";
        return jdbcTemplate.query(sql, this::transform, name);
    }

    @Override
    public boolean unavailableUser(int id) {
        String sql = "update t_sys_user set status = 2 where id = ? ";
        return jdbcTemplate.update(sql, id) == 1;//影响行数为1
    }

    @Override
    public boolean insertIdentify(int user_id, int role_id) {
        String sql = "insert into t_sys_user_role(user_id, role_id) values (?, ?) ";
        return jdbcTemplate.update(sql, user_id, role_id) == 1;
    }

    @Override
    public List<Permission> getUserPermission(String user_id) {
        String sql = "select `id`,`name`,`code`,`url`,`order` from t_sys_permission where id in (" +
                "select perm_id from t_sys_role_permission where role_id in (" +
                "select role_id from t_sys_user_role where user_id = ?" +
                ")" +
                ");";
        return jdbcTemplate.query(sql, this::PermissionTransform, user_id);
    }

    @Override
    public boolean isAllowPermission(int user_id, int perm_id) {
        String sql = "select count(*) from t_sys_role_permission where perm_id = ? and" +
                " role_id in (select role_id from t_sys_user_role where user_id = ? )";
        return jdbcTemplate.queryForObject(sql, Integer.class, perm_id, user_id) > 0;
    }

    @Override
    public Permission PermissionTransform(ResultSet rs, int RowNum) throws SQLException {
        Permission permission = new Permission();
        permission.setId(rs.getInt("id"));
        permission.setName(rs.getString("name"));
        permission.setCode(rs.getString("code"));
        permission.setUrl(rs.getString("url"));
        permission.setOrder(rs.getString("order"));
        return permission;
    }

    @Override
    public int queryRoleIdByName(String name) {
        String sql = "select id from t_sys_role where name = ? ;";
        try {
            return jdbcTemplate.queryForObject(sql, Integer.class, name);
        } catch (Exception e) {
            return -1;
        }
    }

    @Override
    public boolean isExitPermById(int perm_id) {
        String sql = "select count(*) from t_sys_permission where id = ?";
        return jdbcTemplate.queryForObject(sql, Integer.class, perm_id) > 0;
    }

    @Override
    public boolean isExitRoleByName(String name) {
        String sql = "select count(*) from t_sys_role where name = ?";
        return jdbcTemplate.queryForObject(sql, Integer.class, name) > 0;
    }

    @Override
    public User transform(ResultSet rs, int RowNum) throws SQLException {
        return new User(
                rs.getInt("id"),
                rs.getString("login_name"),
                rs.getString("password"),
                rs.getString("gender"),
                rs.getDate("birthday"),
                rs.getString("phone"),
                rs.getString("email"),
                rs.getInt("status")
        );
    }

}
